//
//  ReceiveViewController.m
//  Zhiche-Carriage
//
//  Created by LeeBruce on 16/6/6.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//


#import "ReceiveViewController.h"
#import "RootViewController.h"
#import "Common.h"
#import <MJRefresh/MJRefresh.h>
#import "WKProgressHUD.h"
#import "RLNetworkHelper.h"
#import "NullDataView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "LoginViewController.h"
#import "ImageWebVC.h"
#import <Masonry/Masonry.h>
#import "TimePickerView.h"
#import "SDCycleScrollView.h"
#import "FTPopOverMenu.h"

//#import "NewBillVC.h"
#import "DrivingRecordVC.h"
#import "RecordVC.h"



#define threeWidth  Main_Width/2

@interface ReceiveViewController ()<UITextFieldDelegate,SDCycleScrollViewDelegate>
{
    UIView * nav;
    Common * com;

    UIView * NoNetWorkView;
    NullDataView * nullView;
    
    //滚动视图的对象指针
    UIScrollView * ScrollView;
    //分页控件的对象指针
    NSTimer * timer;
    NSMutableDictionary * userDic;
    NSMutableArray * scrollArr;

}


@property (nonatomic,strong) UIView * backView;
@property (nonatomic,strong) UIImageView *backImageView;
//@property (nonatomic,strong) UIButton *startAddressLabel;
//@property (nonatomic,strong) UIButton *endAddressLabel;
@property (nonatomic,strong) UIButton *choiceButton;
@property (nonatomic,strong) UIButton *timeButton;
@property (nonatomic,strong) UITextField *orderField;
@property (nonatomic,strong) UIView *backgroundView;
@property (nonatomic,retain) TimePickerView *timeView;
@property (nonatomic,strong) NSMutableDictionary *postDic;

@property (nonatomic,strong) NSMutableArray *cityArray;

@end

@implementation ReceiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createInit];
    nav = [self createNavView];
    [self createNavTitle:@"慧运车"];
    [self.view addSubview:nav];
    self.view.backgroundColor = GrayColor;
    
      nullView = [[NullDataView alloc]initWithFrame:CGRectMake(0, 0, Main_Width, Main_height-64) andTitle:@"暂无运单" andImageName:@"暂无待接单"];
    nullView.backgroundColor = GrayColor;
    
    [self initPostDic];
    [self createData];

    [self createThreeBtn];
    [self createInfoView];


}

-(void)initPostDic
{
    self.cityArray = [NSMutableArray array];
    
    self.postDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"",@"codes",@"",@"names",@"",@"capacity",@"",@"estimatedTime",@"",@"routeId", nil];
}

-(void)createThreeBtn{
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"出车记录" forState:UIControlStateNormal];
    [rightButton setTitleColor:WhiteColor forState:UIControlStateNormal];
    rightButton.titleLabel.font = Font(13);
    [rightButton addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    rightButton.frame = CGRectMake(screenWidth - 90, 20, 72, 44);
    [self.view addSubview:rightButton];
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    
    ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 63.5, screenWidth, screenHeight  - 63)];
    ScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    ScrollView.bounces = YES;
    [self.view addSubview:ScrollView];
    
   
    /*
    
    self.backImageView = [[UIImageView alloc]init];
    UIImageView * imageline = [[UIImageView alloc]initWithFrame:CGRectMake(0,48, Main_Width, 0.5)];
    imageline.backgroundColor = AddCarNameBtnColor;
    [self.backImageView addSubview:imageline];
    self.backImageView.backgroundColor = [UIColor whiteColor];
    self.backImageView.userInteractionEnabled = YES;
    [ScrollView addSubview:self.backImageView];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(64 + 164 * kHeight);
        make.left.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(screenWidth,60*kHeight));
    }];
    [imageline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.backImageView.mas_bottom);
        make.left.mas_equalTo(self.backImageView.mas_left);
        make.size.mas_equalTo(CGSizeMake(Main_Width,0.5));
    }];

//    NSArray *titleArray = @[@"我的账单",@"账户余额",@"出车记录"];
    NSArray *titleArray = @[@"我的账单",@"出车记录"];

//    CGFloat space = screenWidth / 2.0;
    for (int i = 0; i < titleArray.count; i++) {
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        NSString * imageNormal = [NSString stringWithFormat:@"账单"];
        if (i ==0) {
            [button setImage:[UIImage imageNamed:imageNormal] forState:UIControlStateNormal];
        }
        else {
            NSString * number = [NSString stringWithFormat:@"%@",userDic[@"doneCount"]];
            [button setTitle:number forState:UIControlStateNormal];
        }
        
        [button setTitleColor:YellowColor forState:UIControlStateNormal];
        button.titleLabel.font = Font(FontOfSize12);
        button.tag = 1001+i+100;
        UIButton * buttonBig = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonBig.tag = 1001+i;
        
        [self.backImageView addSubview:buttonBig];
        [buttonBig mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.backImageView.mas_top).with.offset(8*kHeight);
            make.left.mas_equalTo(self.backImageView.mas_left).with.offset(threeWidth*i);
            make.size.mas_equalTo(CGSizeMake(threeWidth, 46*kHeight));
        }];
        
        [button addTarget:self action:@selector(pressBig:) forControlEvents:UIControlEventTouchUpInside];
        [buttonBig addTarget:self action:@selector(pressBigButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.backImageView addSubview:button];
        
        
        if (i==1) {
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.backImageView.mas_top).with.offset(10*kHeight);
//                make.centerX.mas_equalTo(self.backImageView.mas_centerX);
                make.centerX.mas_equalTo(buttonBig.mas_centerX);

                make.size.mas_equalTo(CGSizeMake(50*kWidth, 21*kHeight));
            }];
        }else{
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.backImageView.mas_top).with.offset(10*kHeight);
//                make.left.mas_equalTo(self.backImageView.mas_left).with.offset(40*kWidth+i*space);
                make.centerX.mas_equalTo(buttonBig.mas_centerX);
                make.size.mas_equalTo(CGSizeMake(15*kWidth, 21*kHeight));
            }];
        }
        
        UILabel * label = [self createUIlabel:titleArray[i] andFont:FontOfSize12 andColor:AddressSCtitleColor];
        label.text = titleArray[i];
        

        [self.backImageView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(button.mas_bottom).with.offset(2*kHeight);
            make.centerX.mas_equalTo(buttonBig.mas_centerX);
            
        }];

          }
     
     */
}
//出车记录
-(void)rightButtonAction
{
    DrivingRecordVC * Driving = [[DrivingRecordVC alloc]init];
    [Common isLogin:self andPush:Driving];
}


#pragma mark 地点 时间 运单数
-(void)createInfoView
{
    __weak typeof(self) weakSelf = self;
    
    UIView *infoView = [[UIView alloc]init];
    [ScrollView addSubview:infoView];
    infoView.backgroundColor = WhiteColor;
    [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(screenWidth);
//        make.top.mas_equalTo(weakSelf.backImageView.mas_bottom).offset(5);
        make.top.mas_equalTo( 169 * kHeight);

        make.bottom.mas_equalTo(weakSelf.view.mas_bottom).offset(-53 * kHeight);
    
    }];
    
    //线路
    UIImageView *lineImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"first_line"]];
    [infoView addSubview:lineImg];
    [lineImg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(15);
        make.top.mas_equalTo((cellHeight - 19)/2);
        make.size.mas_equalTo(CGSizeMake(20, 19));
        
    }];
    
    UILabel *lineLabel = [[UILabel alloc]init];
    [infoView addSubview:lineLabel];
    lineLabel.text = @"线路";
    lineLabel.textColor = AddressSCtitleColor;
    lineLabel.font = Font(13);
    [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(lineImg.mas_right).offset(10);
        make.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(80, cellHeight));
        
    }];
    
    
    self.choiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [infoView addSubview:self.choiceButton];
    [self.choiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(lineLabel.mas_right).offset(0);
        make.top.mas_equalTo(10);
        make.right.mas_equalTo(-70);
        make.height.mas_equalTo(cellHeight - 20);
        
    }];
    self.choiceButton.titleLabel.font = Font(12);
    [self.choiceButton setTitleColor:littleBlackColor forState:UIControlStateNormal];
    self.choiceButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.choiceButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

    [self.choiceButton addTarget:self action:@selector(choiceStartCity:) forControlEvents:UIControlEventTouchUpInside];
    self.choiceButton.layer.borderColor = GrayColor.CGColor;
    self.choiceButton.layer.borderWidth = 0.5;

    
    //黑三角
    UIImageView *triangle = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"triangle"]];
    [self.choiceButton addSubview:triangle];
    [triangle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-5);
        make.centerY.equalTo(weakSelf.choiceButton);
        make.size.mas_equalTo(CGSizeMake(8.5, 6));
        
    }];

    
    
    UILabel *lineL = [[UILabel alloc]init];
    [infoView addSubview:lineL];
    lineL.backgroundColor = LineGrayColor;
    [lineL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(cellHeight);
        make.size.mas_equalTo(CGSizeMake(screenWidth - 10, 0.5 ));
        
    }];
    
    
    //选择时间
    
    UILabel *timeLabel = [[UILabel alloc]init];
    [infoView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(lineLabel);
        make.top.mas_equalTo(lineL.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(80, cellHeight));
        
    }];
    timeLabel.text = @"选择时间";
    timeLabel.font = Font(13);
    timeLabel.textColor = AddressSCtitleColor;
    
    
    UIImageView *timeImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"time"]];
    [infoView addSubview:timeImg];
    [timeImg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(15);
        make.size.mas_equalTo(CGSizeMake(17, 17));
        make.centerY.equalTo(timeLabel);
    }];

    
    UILabel *lineL1 = [[UILabel alloc]init];
    [infoView addSubview:lineL1];
    lineL1.backgroundColor = LineGrayColor;
    [lineL1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(10);
        make.top.equalTo(timeLabel.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(screenWidth - 10, 0.5 ));
        
    }];
    
    
    self.timeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [infoView addSubview:self.timeButton];
    [self.timeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(timeLabel.mas_right).offset(10);
        make.height.top.equalTo(timeLabel);
        make.right.mas_equalTo(0);
        
    }];
    self.timeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.timeButton.titleLabel.font = Font(13);
    [self.timeButton setTitleColor:littleBlackColor forState:UIControlStateNormal];
    
    [self.timeButton addTarget:self action:@selector(choiceDate) forControlEvents:UIControlEventTouchUpInside];
    
    //初始化button显示时间
    NSDateFormatter *forma = [[NSDateFormatter alloc]init];
    
    [forma setDateFormat:@"YYYY-MM-dd"];
    NSDate *maxDate = [[NSDate alloc]initWithTimeIntervalSinceNow:24*60*60 ];

    [self.timeButton setTitle:[forma stringFromDate: maxDate] forState:UIControlStateNormal] ;
    self.timeButton.userInteractionEnabled = NO;
    
    
    //初始化上传日期
    [self.postDic setObject:[self dateStringWith:[forma stringFromDate: maxDate]] forKey:@"estimatedTime"];

    
    
    //运单数
    
    UILabel *orderLabel = [[UILabel alloc]init];
    [infoView addSubview:orderLabel];
    [orderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(timeLabel);
        make.top.mas_equalTo(timeLabel.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(80, cellHeight));
        
    }];
    orderLabel.text = @"运单数";
    orderLabel.font = Font(13);
    orderLabel.textColor = AddressSCtitleColor;
    
    
    UIImageView *orderImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"first_order"]];
    [infoView addSubview:orderImg];
    [orderImg mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(timeImg);
        make.size.mas_equalTo(CGSizeMake(13.5, 17));
        make.centerY.equalTo(orderLabel);
    }];
    
    
    UILabel *lineL2 = [[UILabel alloc]init];
    [infoView addSubview:lineL2];
    lineL2.backgroundColor = LineGrayColor;
    [lineL2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(10);
        make.top.equalTo(orderLabel.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(screenWidth - 10, 0.5 ));
        
    }];
    

    //请输入运单数
    self.orderField = [[UITextField alloc]init];
    [infoView addSubview:self.orderField];
    [self.orderField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(timeLabel.mas_right).offset(10);
        make.height.top.equalTo(orderLabel);
        make.right.mas_equalTo(0);
        
    }];
    self.orderField.font = [UIFont systemFontOfSize:14];
    self.orderField.placeholder = @"请输入运单数";
    self.orderField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.orderField.delegate = self;
    self.orderField.returnKeyType = UIReturnKeyDone;
    [self.orderField setValue:carScrollColor forKeyPath:@"_placeholderLabel.textColor"];
//    需要和textfield字体大小一致
    [self.orderField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    
    UIButton *postButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [infoView addSubview:postButton];
    [postButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(35);
        make.bottom.mas_equalTo(infoView.mas_bottom).offset(-45);
        make.right.mas_equalTo(-35);
        make.height.mas_equalTo(33 * kHeight);
        
    }];
    [postButton setTitle:@"提交申请" forState:UIControlStateNormal];
    [postButton setBackgroundColor:YellowColor];
    [postButton setTitleColor:WhiteColor forState:UIControlStateNormal];
    postButton.titleLabel.font = Font(14);
    postButton.layer.cornerRadius = 5;
    [postButton addTarget:self action:@selector(postButtonActino) forControlEvents:UIControlEventTouchUpInside];
    
}
//选择城市
-(void)choiceStartCity:(UIButton *)sender
{
    
    __weak typeof(self) weakSelf = self;
    
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < self.cityArray.count; i ++) {
        
        [arr addObject:self.cityArray[i][@"route"] ];
        
    }
    
    if (self.cityArray.count != 0) {
        
       [FTPopOverMenu showForSender:sender
                        withMenu:arr
                  imageNameArray:nil
                       doneBlock:^(NSInteger selectedIndex) {
                           
                           [weakSelf.choiceButton setTitle:self.cityArray[selectedIndex][@"route"] forState:UIControlStateNormal];
                           [weakSelf.postDic setObject:self.cityArray[selectedIndex][@"code"] forKey:@"codes"];
                           
                           [weakSelf.postDic setObject:self.cityArray[selectedIndex][@"route"] forKey:@"names"];
                           [weakSelf.postDic setObject:self.cityArray[selectedIndex][@"id"] forKey:@"routeId"];

                           
                       } dismissBlock:^{
                           
                       }];
    
    } else {
        
        
    }
    
   }


//提交信息
-(void)postButtonActino
{
    

    NSArray *arr = [self.postDic allValues];
    if ([arr containsObject:@""]) {
        
        [WKProgressHUD popMessage:@"请将信息补充完整" inView:self.view duration:1.5 animated:YES];

        
    } else {
    
    //上报运力
    Common *c = [[Common alloc]init];
     [c afPostRequestWithUrlString:sub_cwlist parms:self.postDic finishedBlock:^(id responseObj) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:nil];
         
         [WKProgressHUD popMessage:dic[@"message"] inView:self.view duration:1.5 animated:YES];
         
         if ([dic[@"success"] boolValue]) {
             
//             NSDateFormatter *forma = [[NSDateFormatter alloc]init];
//             
//             [forma setDateFormat:@"YYYY-MM-dd"];
//             
//             [self.timeButton setTitle:[forma stringFromDate:[NSDate date]] forState:UIControlStateNormal];
//             [self.postDic setObject:[forma stringFromDate:[NSDate date]] forKey:@"estimatedTime"];
             self.orderField.text = nil;
             
             [self.postDic setObject:@"" forKey:@"capacity"];
             

         }
         
        
    } failedBlock:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];
    }
    
}


-(void)pressBig:(UIButton *)sender
{
    UIButton *button = [self.view viewWithTag:sender.tag - 100];
    
    [self pressBigButton:button];
}

-(void)pressBigButton:(UIButton*)sender{
    
    NSInteger tag = sender.tag;
    switch (tag) {
        case 1001://我的账单
        {
            RecordVC * billvc = [[RecordVC alloc]init];
            [Common isLogin:self andPush:billvc];
        }
            break;
//        case 1002://账户余额
//        {
////            MyWalletViewController * wall = [[MyWalletViewController alloc]init];
////            [Common isLogin:self andPush:wall];
//            break;
//        }
        case 1002://出车记录
        {
            DrivingRecordVC * Driving = [[DrivingRecordVC alloc]init];
            [Common isLogin:self andPush:Driving];
            break;
        }
            
        default:
            break;
    }
}

-(void)createData{
    
    //轮播图
    NSString * string = [NSString stringWithFormat:@"%@?clienttype=30&apptype=40",banner_list_Url];
    [Common requestWithUrlString:string contentType:@"application/json" finished:^(id responseObj) {
        
        int success = [responseObj[@"success"] intValue];
        if (success == 1) {
            scrollArr = responseObj[@"data"];
            [self createScrollImage];
        }
        
    } failed:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];
    
}


-(void)createScrollImage{

    NSMutableArray *arr = [NSMutableArray array];
    NSMutableArray *urlArr = [NSMutableArray array];
    for (int i = 0; i < scrollArr.count; i++) {
        [arr addObject:scrollArr[i][@"picKey"]];
        
        [urlArr addObject:scrollArr[i][@"picHref"]];
    }
    
      
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, screenWidth, 164 * kHeight) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
//    cycleScrollView.titlesGroup = titles;
    cycleScrollView.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    [ScrollView addSubview:cycleScrollView];
    cycleScrollView.urlArr = urlArr;
    
    //         --- 模拟加载延迟
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScrollView.imageURLStringsGroup = arr;
        
    });

    
}

-(void)tapAction:(UITapGestureRecognizer *)tap{
    
    ImageWebVC * imageweb = [[ImageWebVC alloc]init];
    NSString * urlImage = [NSString stringWithFormat:@"%@",scrollArr[tap.view.tag][@"picHref"]];
    if (urlImage.length>0) {
        imageweb.imageURL = urlImage;
        [self.navigationController pushViewController:imageweb animated:YES];
    }
}



-(void)createInit{

    com = [[Common alloc]init];

}

-(void)createNetWorkAndStatue{
    //创建无网络状态view
    NoNetWorkView = [com noInternet:@"网络异常" and:self action:@selector(NoNetPressBtn) andCGreck:CGRectMake(0, 0, Main_Width, Main_height-64)];

    //判断是否有网
    [self judgeNetWork];
}

-(void)NoNetPressBtn{
    [self judgeNetWork];
}


//判断是否有网
-(void)judgeNetWork{
    if (![RLNetworkHelper isConnectedToNetwork]) {
        NoNetWorkView.hidden = NO;
    }
    else{
        NoNetWorkView.hidden = YES;
        UIButton * btn1 = (UIButton*)[self.view viewWithTag:1];
        if (btn1.selected) {
        }else{

        }
    }
}


-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES];
    [_backView removeFromSuperview];
    RootViewController *rootVC = [RootViewController defaultsTabBar];
    [rootVC setTabBarHidden:NO];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString *string = [userDefaults objectForKey:@"login"];
    
    if (string == nil ) {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        UINavigationController * loginNC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        [self presentViewController:loginNC animated:YES completion:nil];
        
    }else{
        //用户状态 出车记录 余额
        
         NSString * string = [NSString stringWithFormat:@"%@?pageNo=1&pageSize=10",waybill_done_Url];
        
        [Common requestWithUrlString:string contentType:@"application/json" finished:^(id responseObj){
            
            NSString * userStatus = [NSString stringWithFormat:@"%@",responseObj[@"success"]];
            if ([userStatus isEqualToString:@"1"]) {
                
                userDic = responseObj[@"data"];

                UIButton * btnnumber = (UIButton*)[self.backImageView viewWithTag:1102];
                
                NSString * doneCount = [NSString stringWithFormat:@"%@",userDic[@"page"][@"totalRecord"]];
                [btnnumber setTitle:doneCount forState:UIControlStateNormal];
                
            }
            
            
        } failed:^(NSString *errorMsg) {
            NSLog(@"%@",errorMsg);
        }];
        
        
            //城市列表
            [Common requestWithUrlString:sub_routes contentType:application_json finished:^(id responseObj) {
                
                if ([responseObj[@"data"] isEqual:[NSNull null]]) {
                    
                } else {
                    self.cityArray = [NSMutableArray arrayWithArray:responseObj[@"data"]];
                    
                }
                
                if (self.cityArray.count >0) {
                    [self.choiceButton setTitle:self.cityArray[0][@"route"] forState:UIControlStateNormal];
                    
                    //默认值
                    [self.postDic setObject:self.cityArray[0][@"code"] forKey:@"codes"];
                    
                    [self.postDic setObject:self.cityArray[0][@"route"] forKey:@"names"];
                    [self.postDic setObject:self.cityArray[0][@"id"] forKey:@"routeId"];

                    
                }
                
                
            } failed:^(NSString *errorMsg) {
                
            }];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(UILabel*)createUIlabel:(NSString *)title andFont:(CGFloat)font andColor:(UIColor *)color{
    
    UILabel * label =[[UILabel alloc]init];
    label.text = title;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = color;
    UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:font];CGSize size = [label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
    // 名字的H
    CGFloat nameH = size.height;
    // 名字的W
    CGFloat nameW = size.width;
    
    label.textAlignment = NSTextAlignmentLeft;
    label.frame =CGRectMake(0, 0, nameW, nameH);
    label.font = Font(font)
    return label;
}
-(NSString *)backString:(NSString *)string
{
    //    NSString * str = [NSString stringWithFormat:@"%@",string];
    if ([string isEqual:[NSNull null]]) {
        return @"";
    } else {
        return string;
    }
}

//点击return 按钮 去掉
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//日期替换  2016年－12月－12日  2016-12-12
-(NSString *)dateStringWith:(NSString *)str
{
    
  str = [str stringByReplacingOccurrencesOfString:@"年" withString:@""];
   str =  [str stringByReplacingOccurrencesOfString:@"月" withString:@""];
   str =  [str stringByReplacingOccurrencesOfString:@"日" withString:@""];
    return  str;

}

#pragma mark 选择时间
-(void)choiceDate
{
    [self.view endEditing:YES];
  
    __weak typeof(self) weakSelf = self;
    
   self.backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    
    self.timeView = [[TimePickerView alloc]initWithFrame:CGRectMake(0, screenHeight, screenWidth, screenHeight)];
    self.timeView.datePicker.minimumDate = [NSDate date];

    //3天范围
    NSDate *maxDate = [[NSDate alloc]initWithTimeIntervalSinceNow:24*60*60 * 3];
    self.timeView.datePicker.maximumDate = maxDate;
    
    self.timeView.value = self.timeButton.titleLabel.text;
    
    self.timeView.backgroundColor = WhiteColor;
    self.timeView.callBack = ^(NSString *str){
        
        if (str == nil) {
            
            
        } else {
            [weakSelf.timeButton setTitle:str forState:UIControlStateNormal];
            [weakSelf.postDic setObject:[weakSelf dateStringWith:str] forKey:@"estimatedTime"];
        }
        
        
     [UIView animateWithDuration:0.4 animations:^{
         
         [weakSelf.backgroundView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];

     weakSelf.timeView.frame = CGRectMake(0, screenHeight  , screenWidth, 200);

     } completion:^(BOOL finished) {
         [weakSelf.backgroundView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1]];

         [weakSelf.backgroundView removeFromSuperview];
     }];
    };
    
    
//    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];

    
    [UIView animateWithDuration:0.4 animations:^{
        
        [weakSelf.backgroundView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];
        
       weakSelf.timeView.frame = CGRectMake(0, screenHeight - 200, screenWidth, 200);

        [window addSubview:weakSelf.backgroundView];
        [weakSelf.backgroundView addSubview:weakSelf.timeView];

        

    } completion:^(BOOL finished) {
//        [self.backgroundView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];

    }];
    
}



-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.3 animations:^{
        
        ScrollView.contentInset = UIEdgeInsetsMake(-50, 0, 0, 0);
        ScrollView.contentSize = CGSizeMake(screenWidth, screenHeight + 51);
        
    } completion:^(BOOL finished) {
        
    }];
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
    [self.postDic setObject:_orderField.text forKey:@"capacity"];

    
    [UIView animateWithDuration:0.3 animations:^{
        
        ScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        ScrollView.contentSize = CGSizeMake(screenWidth, screenHeight - 63 );

        
    } completion:^(BOOL finished) {
        
    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField.text.length > 2) {
        
        [WKProgressHUD popMessage:@"运单数位数不能超过四位数" inView:self.view duration:1.5 animated:YES];
        
        textField.text = nil;
        [self.postDic setObject:@"" forKey:@"capacity"];

        
        return NO;
        
    } else {
        
        
    return [self validateNumber:string];
    }
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
        
//        [self.postDic setObject:_orderField.text forKey:@"capacity"];

    }
    
    
    return res;
}

-(void)viewDidAppear:(BOOL)animated
{
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;

}



@end
